<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* CSVReader Class
*
* $Id: csvreader.php 147 2007-07-09 23:12:45Z Pierre-Jean $
*
* Allows to retrieve a CSV file content as a two dimensional array.
* The first text line shall contains the column names.
*
* Let's consider the following CSV formatted data:
*
*        col1;col2;col3
*         11;12;13
*         21;22;23
*
* It's returned as follow by the parsing operations:
*
*         Array(
*             [0] => Array(
*                     [col1] => 11,
*                     [col2] => 12,
*                     [col3] => 13
*             )
*             [1] => Array(
*                     [col1] => 21,
*                     [col2] => 22,
*                     [col3] => 23
*             )
*        )
*
* @author        Pierre-Jean Turpeau
* @link        http://www.codeigniter.com/wiki/CSVReader
*/
class CSVReader {
    
    var $fields;        /** columns names retrieved after parsing */
    var $separator 	= ';';    /** separator used to explode each line */
	var $path 		= '';
	var $extn 		= '.csv';
	
	/**
	* Constructor
	*
	* @access    public
	* @param    array    initialization parameters
	*/
	function File_Uploader($params = array())
	{
		//$CI =& get_instance();		
		if (count($params) > 0)
		{
			$this->initialize($params);        
		}
	
		log_message('debug', "CSVReader Class Initialized");
	}
	
	// --------------------------------------------------------------------
    
    /**
     * Initialize Preferences
     *
     * @access    public
     * @param     array    initialization parameters
     * @return    void
     */
    function initialize($params = array())
    {
        if (count($params) > 0)
        {
            foreach ($params as $key => $val)
            {
                if (isset($this->$key))
                {
                    $this->$key = $val;
                }
            }        
        }
    }
	
	// ------------------------------------------------------------------------
	
    /**
     * Parse a text containing CSV formatted data.
     *
     * @access    public
     * @param    string
     * @return    array
     */
    function parse_text($p_Text) {
        $lines = explode("\n", $p_Text);
        return $this->parse_lines($lines);
    }
	
	// ------------------------------------------------------------------------
    
    /**
     * Parse a file containing CSV formatted data.
     *
     * @access    public
     * @param    string
     * @return    array
     */
    function parse_file($p_Filepath) {
        $lines = file($p_Filepath);
        return $this->parse_lines($lines);
    }
    
	// ------------------------------------------------------------------------
	
    /**
     * Parse an array of text lines containing CSV formatted data.
     *
     * @access    public
     * @param    array
     * @return    array
     */
    function parse_lines($p_CSVLines) {    
        $content = FALSE;
        foreach( $p_CSVLines as $line_num => $line ) {
            if( $line != '' ) { // skip empty lines
                $elements = split($this->separator, $line);								

                if( !is_array($content) ) { // the first line contains fields names
                    $this->fields = $elements;
                    $content = array();
                } else {
                    $item = array();
                    foreach( $this->fields as $id => $field ) {
                        if( isset($elements[$id]) ) {
                            $item[trim($field)] = $elements[$id];
                        }
                    }
                    $content[] = $item;
                }
            }
        }
        return $content;
    }
	
	// ------------------------------------------------------------------------

	/**
	 * Array to CSV
	 *
	 * @access    public
     * @param    array
	 * @param    string
     * @return    void
	 * download == "" -> return CSV string
	 * download == "toto.csv" -> download file toto.csv
	 * 
	 * Example: array_to_csv($array); or array_to_csv($array, 'toto.csv');
	 */
	
	function array_to_csv($array, $download = "")
	{			
		// store the csv file as a report in the system
		$f = fopen($this->path . $download . $this->extn, 'w') or show_error("Can't open php://output");
		$n = 0;		
		foreach ($array as $line)
		{
			$n++;
			if ( ! fputcsv($f, $line))
			{
				show_error("Can't write line $n: $line");
			}
		}
		fclose($f) or show_error("Can't close php://output");
		
		// get all the contents from the file and put it into a variable
		ob_start();
		include($this->path . $download . $this->extn);
		$str = ob_get_contents();
		ob_end_clean();

		if ($download == "")
		{
			return $str;	
		}
		else
		{	
			header('Content-Type: application/csv');
			header('Content-Disposition: attachement; filename="' . $download . $this->extn .'"');
			echo $str;
		}		
	}
	
	
	// ------------------------------------------------------------------------
	
	/**
	 * Query to CSV
	 *
	 * @access  public
     * @param   query object
	 * @param   boolean
	 * @param   string
     * @return  void
	 * download == "" -> return CSV string
	 * download == "toto.csv" -> download file toto.csv
	 * 
	 * Example: query_to_csv($query, TRUE); or query_to_csv($query, TRUE, 'toto.csv');
	 */
	
	function query_to_csv($query, $headers = TRUE, $download = "")
	{
		if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
		{
			show_error('invalid query');
		}
		
		$array = array();
		
		if ($headers)
		{
			$line = array();
			foreach ($query->list_fields() as $name)
			{
				$line[] = $name;
			}
			$array[] = $line;
		}
		
		foreach ($query->result_array() as $row)
		{
			$line = array();
			foreach ($row as $item)
			{
				$line[] = $item;
			}
			$array[] = $line;
		}

		$this->array_to_csv($array, $download);
	}
	
}
/* End of file CSVReader.php */
/* Location: ./system/application/libraries/CSVReader.php */
?>